# 文明网统计
from requests_html import HTMLSession
from datetime import datetime, timedelta
import pandas as pd
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart  # 创建附件类型
from email.mime.application import MIMEApplication#发送附件
from email import encoders
import time


MAIL_FROM = "chenliangxu68@163.com"
SMTP_SERVER = "smtp.163.com"
MAIL_PW = "163$323428"
FW_URL = 'http://foshanplus.hzfanews.com'
FW_USERID = '3231138@qq.com'
FW_PASSWD = '18170947173'

path = "/home/louis/Desktop/"

session = HTMLSession()

def send_mail(mail_to, subject, msg_txt, file=None):
    # Record the MIME types of both parts - text/plain and text/html.

    msg = MIMEMultipart()
    msg['Subject'] = subject
    msg['From'] = MAIL_FROM
    msg['To'] = mail_to
    msg_text = MIMEText(msg_txt, 'html', 'utf-8')
    msg.attach(msg_text)

    if file:
        file_name = file.split('/')[-1]
        with open(file, 'rb') as f:
            part_attach1 = MIMEApplication(f.read())  # 打开附件
            part_attach1.add_header('Content-Disposition', 'attachment', filename=file_name)  # 为附件命名
            encoders.encode_base64(part_attach1)
            msg.attach(part_attach1)  # 添加附件

    server = smtplib.SMTP(SMTP_SERVER, 25)
    try:
        server.login(MAIL_FROM, MAIL_PW)
        mailto_list = mail_to.strip().split(",")
        if len(mailto_list) > 1:
            for mailtoi in mailto_list:
                server.sendmail(MAIL_FROM, mailtoi.strip(), msg.as_string())
        else:
            server.sendmail(MAIL_FROM, mail_to, msg.as_string())
    except Exception as e:
        print(e)
        server.quit()
        return False

    server.quit()
    return True

def login_page():
    # 后台登录
    url = 'http://foshanplus.hzfanews.com/v2/default.aspx'
    data = {
        "tbUserID": FW_USERID,
        "tbPwd": FW_PASSWD,
        "btnLogin": "登录",
    }
    res = session.get(url)
    form = res.html.find('form', first=True)
    auth_url  = 'http://foshanplus.hzfanews.com/UserManage/UserLogin/' + form.attrs['action'][2:]

    v1 = form.find('#__VIEWSTATE', first=True)
    data['__VIEWSTATE'] = v1.attrs['value']

    v2 = form.find('#__VIEWSTATEGENERATOR', first=True)
    data['__VIEWSTATEGENERATOR'] = v2.attrs['value']

    session.post(auth_url,data=data)



def get_data(id,startDate,endDate,key):
    url = f'http://foshanplus.hzfanews.com/api/ArticleList.ashx?whatDo=getArticleList&id={id}&start=0&limit=40&' \
        f'startDate={startDate}&endDate={endDate}&orderby=updatetime+desc&sourceType=1&' \
        f'markinfo=1&rd=0.7328814369637415&keyWords={key}'
    res = session.get(url)
    res = res.json()
    if res['Succeed']:
        return res
    else:
        print('login first')
        login_page()
        res = get_data(id=id, startDate=startDate, endDate=endDate,key=key)
        return res

def wenming_static(startDate=None):
    result = {"城市":[], "省份":[], "文章标题":[], "来源":[], "栏目":[], "只看标题":[], "发文时间":[], "地址":[]}
    citys_dict = {
        "广东省":["佛山","广州","深圳","珠海","汕头","韶关","湛江","肇庆","江门","茂名","惠州","梅州","汕尾","河源","阳江",
              "清远","东莞","中山","潮州","揭阳","云浮"],
        "江苏省":["张家港"],
        "福建省":["福州"],
        "安徽省":["亳州"],
        "河南省":["南阳"],
        "云南省":["曲靖"]
    }
    ids_dict = {"20266355": '大图', "20248430":'要闻', "20194679":'地方联播'}
    # id, startDate, endDate, limit, keyword 统计数量
    today = datetime.today()
    if not startDate:
        today = datetime.today()

        if today.month-1 == 0:
            last_month = 12
        elif today.month-1 < 10:
            last_month = f"0{today.month-1}"
        else:
            last_month = f"{today.month-1}"
        startDate = f"2020-{last_month}-01"

    endDate = datetime.strftime(today - timedelta(days=1), "%Y-%m-%d")
    email_text = f"<h3>文明网统计数据</h3><p>开始时间：{startDate}, 结束时间：{endDate}</p>"
    email_text += '<table border="2"><tr><td>城市</td><td>文章总数</td><td>大图</td><td>要闻</td><td>地方联播</td>' \
                  '<td>只看标题文章总数</td><td>大图</td><td>要闻</td><td>地方联播</td></tr>'

    for provence, citys in citys_dict.items():
        for city in citys:
            total = {'total':0} # 看标题和全文的数量统计
            total_stric = {'total':0} # 只看标题的统计
            for id, source_name in ids_dict.items():
                res = get_data(id=id, startDate=startDate, endDate=endDate, key=city)
                total[id] = res['obj']['total']
                total['total'] += res['obj']['total']
                total_stric[id] = 0
                for item in res['obj']['rows']:
                    result['城市'].append(city)
                    result['省份'].append(provence)
                    result['文章标题'].append(item['title'])
                    result['来源'].append(item['page'])
                    result['栏目'].append(source_name)
                    result['发文时间'].append(item['paperdate'])
                    result['地址'].append(item['url'])
                    if city in item['title']:
                        total_stric['total'] += 1
                        total_stric[id] += 1
                        result['只看标题'].append('是')
                    else:
                        result['只看标题'].append('否')
            email_text += f'<tr><td>{city}</td><td>{total["total"]}</td><td>{total["20266355"]}</td>' \
                f'<td>{total["20248430"]}</td><td>{total["20194679"]}</td>' \
                f'<td>{total_stric["total"]}</td><td>{total_stric["20266355"]}</td><td>{total_stric["20248430"]}</td>' \
                f'<td>{total_stric["20194679"]}</td></tr>'
        break
    email_text += '</table>'
    df = pd.core.frame.DataFrame(result)
    file_name = f'{path}{startDate}_{endDate}.xlsx'
    df.to_excel(file_name, encoding="utf_8_sig")
    #send_list = 'chenliangxu68@163.com,446117093@qq.com'
    #time.sleep(5)
    #send_list = 'chenliangxu68@163.com'
    #send_mail(send_list, '文明网统计数据', email_text, file=file_name)
    return file_name, email_text

if __name__ == '__main__':
    file_name, email_text = wenming_static(startDate='2020-01-02')
    time.sleep(3)
    send_list = 'chenliangxu68@163.com'
    send_mail(send_list, '文明网统计数据2', email_text, file=file_name)